package com.mUI;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xike.kyt.R;

public class MyProgressDialog extends Dialog {
	private static MyProgressDialog customProgressDialog = null;

	public MyProgressDialog(Context context) {
		super(context);
	}

	public MyProgressDialog(Context context, int theme) {
		super(context, theme);
	}

	public static MyProgressDialog createDialog(Context context) {
		customProgressDialog = new MyProgressDialog(context, R.style.MyProgressDialog);
		customProgressDialog.setContentView(R.layout.ui_myprogressdialog);
		customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

		return customProgressDialog;
	}

	public static MyProgressDialog createDialog(Context context, String message) {
		customProgressDialog = new MyProgressDialog(context, R.style.MyProgressDialog);
		View view = View.inflate(context, R.layout.ui_myprogressdialog, null);
		((TextView) view.findViewById(R.id.progressDialog_tv)).setText(message);
		customProgressDialog.setContentView(view);
		customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

		return customProgressDialog;
	}

	public void onWindowFocusChanged(boolean hasFocus) {
		if (customProgressDialog == null) {
			return;
		}
		ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.progressDialog_img);
		AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
		animationDrawable.start();
	}

	/**
	 * @param context
	 * @WZL wzl200711402@163.com @2012-3-26@下午01:34:02
	 * @description 显示加载进度框
	 */
	public static void show(Context context, String message, boolean cancelable) {
		customProgressDialog = MyProgressDialog.createDialog(context, message);
		if (!customProgressDialog.isShowing()) {
			customProgressDialog.show();
		}
	}

	/**
	 * @param context
	 * @WZL wzl200711402@163.com @2012-3-26@下午01:34:02
	 * @description 显示加载进度框
	 */
	public static void show(Context context, boolean cancelable) {
		customProgressDialog = MyProgressDialog.createDialog(context);
		customProgressDialog.setCancelable(cancelable);
		customProgressDialog.setCanceledOnTouchOutside(false);
		if (!customProgressDialog.isShowing()) {
			customProgressDialog.show();
		}
	}

	/**
	 * 
	 * @WZL wzl200711402@163.com @2012-3-30@下午04:22:02
	 * @description 隐藏加载进度框
	 */
	public static void cancle() {
		if (customProgressDialog != null && customProgressDialog.isShowing()) {
			customProgressDialog.dismiss();
			customProgressDialog = null;
		}

	}
}